<?php
function displayCourseContent($atts)
{
	global $wpdb;

	//Display course details
	//Display course details
	$courseId= $_GET['cid'];
	
	if(!is_null($courseId) && $_GET['cmd']=="getCourse")
	{
		$result = $wpdb->get_row("SELECT * FROM g_courses WHERE course_id=".$courseId);
		
		$users = $wpdb->get_results("select distinct staff_id from g_staff_course where course_id=".$courseId);

		$returnString = "<div class='courseName'><h1>". $result ->name."</h1></div>".
				"<div class='courseYear'><h2>  Year: ". $result ->year."</h2></div>".
				"<div class='courseSemester'><h2> Semester: ". $result ->semester."</h2></div>";
		$returnString .="<div><h2>Teaching Staff: ";
		foreach ($users as $user)
		{
			$returnString .= get_the_author_meta("user_firstname",$user->staff_id)." ".
					get_the_author_meta("user_lastname",$user->staff_id).
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}	
		$returnString .="</h2></div>".
		"<div class='courseURL'>".$result ->content."</div>";
		return $returnString;
	}

	//Display Lecture details
	if(!is_null($courseId)&& $_GET['cmd']=="getLecture" )
	{
		$results = $wpdb->get_results("SELECT * FROM g_lectures WHERE course_id=".$courseId." order by date asc");
			
		$returnString = "<div class='lecture'>
		<h1>Lectures</h1>
		<table>";
		$tableRow = "<tr>
		<td>S. No</td>
		<td>Date</td>
		<td>Description</td>
		<td>Slide</td>
		<td>Additional Materials</td>
		</tr>";

		$Sno =1;
		foreach($results as $item)
		{
			$tableRow .= "<tr>
			<td>".$Sno++."</td>
			<td>".$item->date."</td>
			<td>".$item->description."</td>";
			
			if($item->slide=="none")
			{
				$tableRow .= "<td>".$item->slide."</td>";
			}
			else
			{
				$tableRow .= "<td><a href='".$item->slide_url."'>".$item->slide."</a></td>";
			}
			
			if($item->additional=="none")
			{
				$tableRow .= "<td>".$item->additional."</td>";
			}
			else
			{
				$tableRow .= "<td><a href='".$item->additional_url."'>".$item->additional."</a></td>
							</tr>";
			}
			
		}

		$returnString .= $tableRow . "</table></div>";
		return $returnString; 
	}

	if(!is_null($courseId)&& $_GET['cmd']=="getExercise" )
	{
		$results = $wpdb->get_results("SELECT * FROM g_exercises WHERE course_id=".$courseId." order by date asc");
			
		$returnString = "<div class='lecture'>
		<h1>Exercises</h1>
		<table>";
		$tableRow = "<tr>
		<td>S. No</td>
		<td>Date</td>
		<td>Deadline</td>
		<td>Description</td>
		<td>Official Description</td>
		<td>Slide</td>
		<td>Solution</td>
		<td>Additional Materials</td>
		</tr>";
		$Sno =1;
		foreach($results as $item)
		{
			$tableRow .= "<tr>
			<td>".$Sno++."</td>
			<td>".$item->date."</td>
			<td>".$item->deadline."</td>
			<td>".$item->description."</td>";
			
			if($item->file=="none")
			{
				$tableRow .= "<td>".$item->file."</td>";
			}
			else
			{
				$tableRow .= "<td><a href='".$item->file."'>".getFileName($item->file)."</a></td>";
			}
			
			if($item->slide=="none")
			{
				$tableRow .= "<td>".$item->slide."</td>";
			}
			else
			{
				$tableRow .= "<td><a href='".$item->slide."'>".getFileName($item->slide)."</a></td>";
			}
			
			if($item->solution=="none")
			{
				$tableRow .= "<td>".$item->solution."</td>";
			}
			else
			{
				$tableRow .= "<td><a href='".$item->solution."'>".getFileName($item->solution)."</a></td>";
			}
			
			if($item->additional=="none")
			{
				$tableRow .= "<td>".$item->additional."</td>";
			}
			else
			{
				$tableRow .= "<td><a href='".$item->additional_url."'>".getFileName($item->additional)."</a></td>
							</tr>";
			}
		}

		$returnString .= $tableRow . "</table></div>";
		return $returnString; 
	}
	
	if($_GET['mode']=="staff"&&$_GET['uid']!=null)
	{
		$returnString = "<div class='staff'><h1>".
		get_the_author_meta("user_firstname",$_GET['uid'])." ".
		get_the_author_meta("user_lastname",$_GET['uid']).
		"</h1>".
		"<img src='../wp-content/uploads/userphoto/".get_the_author_meta("userphoto_image_file",$_GET['uid'])."'
		alt='".get_the_author_meta("user_firstname",$_GET['uid'])."'>".		
		"</div>".		
		"<div>".get_the_author_meta("description",$_GET['uid'])."</div>".
		"<div> <a href='mailto:".
		get_the_author_meta("user_email",$_GET['uid'])."'>".
		get_the_author_meta("user_email",$_GET['uid'])."</a>".
		"</div>".
		"<div> <a href='".
		get_the_author_meta("user_url",$_GET['uid'])."'>website</a>".
		"</div>";
		
		return $returnString;
	}
	
}

function getFileName($url)
{
	$arr = explode("/",$url);
	return $arr[count($arr)-1];
}
?>
